//! Copyright 2012 Xicut Technology, Inc.
//! All rights reserved
//!
#include "BoardHardware.h"
#include "Config.h"
#include "Sensor.h"

#if defined (__FW_BUILD__)
extern "C"  {
#include "stm32f10x.h"
#include "Config.h"
}
#endif

cSensor::uSensor    cSensor::sSensorStatus;
cSensor::uSensor    cSensor::sSensorChange;
cSysTick            cSensor::sTimeDebounce;

void cSensor::InitReset()
{
#if defined (__FW_BUILD__)
    GPIO_InitTypeDef GPIO_InitStructure;

    RCC_APB2PeriphClockCmd(SW0_PERIPH, ENABLE);
    GPIO_InitStructure.GPIO_Pin = SW0_PIN;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    GPIO_Init(SW0_PORT, &GPIO_InitStructure);

    RCC_APB2PeriphClockCmd(SW1_PERIPH, ENABLE);
    GPIO_InitStructure.GPIO_Pin = SW1_PIN;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    GPIO_Init(SW1_PORT, &GPIO_InitStructure);

    RCC_APB2PeriphClockCmd(SW2_PERIPH, ENABLE);
    GPIO_InitStructure.GPIO_Pin = SW2_PIN;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    GPIO_Init(SW2_PORT, &GPIO_InitStructure);

    RCC_APB2PeriphClockCmd(SW3_PERIPH, ENABLE);
    GPIO_InitStructure.GPIO_Pin = SW3_PIN;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    GPIO_Init(SW3_PORT, &GPIO_InitStructure);

    RCC_APB2PeriphClockCmd(SW4_PERIPH, ENABLE);
    GPIO_InitStructure.GPIO_Pin = SW4_PIN;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    GPIO_Init(SW4_PORT, &GPIO_InitStructure);

    RCC_APB2PeriphClockCmd(SW5_PERIPH, ENABLE);
    GPIO_InitStructure.GPIO_Pin = SW5_PIN;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    GPIO_Init(SW5_PORT, &GPIO_InitStructure);

    RCC_APB2PeriphClockCmd(SW6_PERIPH, ENABLE);
    GPIO_InitStructure.GPIO_Pin = SW6_PIN;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    GPIO_Init(SW6_PORT, &GPIO_InitStructure);

    RCC_APB2PeriphClockCmd(SW7_PERIPH, ENABLE);
    GPIO_InitStructure.GPIO_Pin = SW7_PIN;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    GPIO_Init(SW7_PORT, &GPIO_InitStructure);

    RCC_APB2PeriphClockCmd(SW8_PERIPH, ENABLE);
    GPIO_InitStructure.GPIO_Pin = SW8_PIN;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    GPIO_Init(SW8_PORT, &GPIO_InitStructure);

    RCC_APB2PeriphClockCmd(SW9_PERIPH, ENABLE);
    GPIO_InitStructure.GPIO_Pin = SW9_PIN;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    GPIO_Init(SW9_PORT, &GPIO_InitStructure);
#endif
    UpdateStatus(sSensorStatus);
    sSensorChange.val = 0;
    sTimeDebounce.SetTimeout(cConfig::SS_DEBOUNCE);
    sTimeDebounce.ResetTimeBase();
}


void cSensor::UpdateStatus(uSensor &ss)
{
    ss.val = 0;
    ss.detail.Conveyer_1 = (SS_CONVEYER_1_ACTIVE == SS_CONVEYER_1_PIN)? _TRUE_: _FALSE_;
    ss.detail.Conveyer_2 = (SS_CONVEYER_2_ACTIVE == SS_CONVEYER_2_PIN)? _TRUE_: _FALSE_;
    ss.detail.Conveyer_3 = (SS_CONVEYER_3_ACTIVE == SS_CONVEYER_3_PIN)? _TRUE_: _FALSE_;
    ss.detail.Carbin_1   = (SS_CARBIN_1_ACTIVE == SS_CARBIN_1_PIN)? _TRUE_: _FALSE_;
    ss.detail.Carbin_2   = (SS_CARBIN_2_ACTIVE == SS_CARBIN_2_PIN)? _TRUE_: _FALSE_;
    ss.detail.Carbin_3   = (SS_CARBIN_3_ACTIVE == SS_CARBIN_3_PIN)? _TRUE_: _FALSE_;
    ss.detail.TopCarbin  = (SS_TOP_CARBIN_ACTIVE == SS_TOP_CARBIN_PIN)? _TRUE_: _FALSE_;
    ss.detail.Gate       = (SS_GATE_ACTIVE == SS_GATE_PIN)? _TRUE_: _FALSE_;
    ss.detail.Carbin     = (SS_CARBIN_ACTIVE == SS_CARBIN_PIN)? _TRUE_: _FALSE_;
    ss.detail.ProductOut = (SS_PRODUCT_OUT_ACTIVE == SS_PRODUCT_OUT_PIN)? _TRUE_: _FALSE_;
}


cSensor::uSensor cSensor::GetStatus()
{
    return (sSensorStatus);
}


cSensor::uSensor cSensor::GetChange()
{
    cSensor::uSensor temp = sSensorChange;
    sSensorChange.val = 0;
    return (temp);
}


void cSensor::RunOne()
{
    cSensor::uSensor temp;
    cSensor::uSensor curStatus;

    UpdateStatus(curStatus);
    temp.val = sSensorStatus.val ^ curStatus.val;
    if (0 != temp.val)
    {
        if (true == sTimeDebounce.CheckTimeOut())
        {
            sSensorChange = temp;
            sSensorStatus = curStatus;
        }
    }
    else
    {
        sTimeDebounce.ResetTimeBase();
    }
}

